You can play frequencies defined by any of the three sound data types. By playing a frequency defined by wave-table or sampled-sound data, you can achieve a different sound than by playing that same frequency using square-wave data. For example, you might wish to play the sound of a dog's barking at a variety of frequencies. To do that, however, you need to install a voice of the barking into the sound channel to which you want to send freqCmd or freqDurationCmd commands.
You can install a wave table into a channel as a voice by issuing the waveTableCmd command. The param1 field of the sound command specifies the length of the wave table, and the param2 field is a pointer to the wave-table data itself. Note that the Sound Manager resamples the wave table so that it is exactly 512 bytes long.
You can install a sampled sound into a channel as a voice by issuing the soundCmd command. You can either issue this command from your application or put it into an 'snd ' resource. If your application sends this command, param2 is a pointer to the sampled sound locked in memory. If soundCmd is contained within an 'snd ' resource, the high bit of the command must be set. To use a sampled-sound 'snd ' as a voice, first obtain a pointer to the sampled sound header locked in memory. Then pass this pointer in param2 of a soundCmd command. After using the sound, your application is expected to unlock this resource and allow it to be purged.
Listing 1-26 demonstrates how you can use the soundCmd command to install a sampled sound in memory as a voice in a channel.
Listing 26 Installing a sampled sound as a voice in a channel
FUNCTION MyInstallSampledVoice (mySndHandle: Handle;
mySndChan: SndChannelPtr): OSErr;
VAR
mySndCmd: SndCommand; {a sound command}
mySndHeader: SoundHeaderPtr; {sound header from resource}
BEGIN
{get pointer to sound header}
mySndHeader := MyGetSoundHeader(mySndHandle);
WITH mySndCmd DO
BEGIN
cmd := soundCmd; {install sampled voice}
param1 := 0; {ignored with soundCmd}
param2 := LongInt(mySndHeader); {store sound header location}
END;
IF mySndHeader = NIL THEN {check for defective handle}
MyInstallSampledVoice := badFormat
ELSE {install sound as voice}
MyInstallSampledVoice := SndDoImmediate(mySndChan, mySndCmd);
END;
Listing 1-26 relies on the MyGetSoundHeader function to obtain a pointer to the sound header within the sound handle. That function is defined in "Obtaining a Pointer to a Sound Header" and returns NIL if the sound handle does not include a sound header. Note that the MyGetSoundHeader function locks the sound handle in memory so that the pointer to the sound header remains valid. When you are done with the sound channel in which you have installed the sampled sound, you should unlock the sound handle and make it purgeable so that it does not waste memory.
| Previous | Chapter contents | Chapter top | Section top | Next |